package com.parkwalk.xxx.service;

import org.apache.ibatis.session.SqlSession;

import com.parkwalk.xxx.domain.User;
import com.parkwalk.xxx.persistence.UserMapper;

/**
 * @author borball.zh@gmail.com
 * @date 2012-5-1
 * 
 */
public class UserService extends AbstractService<User, UserMapper> {

	public void updatePassword(User user) {
		SqlSession session = getSqlSession();
		try {
			((UserMapper) session.getMapper(mapperClazz)).updatePassword(user);
		} finally {
			session.commit(true);
			session.close();
		}
	}

	public User findByMail(String mail) {
		SqlSession session = getSqlSession();
		try {
			return getMapper(session).findByMail(mail);
		} finally {
			session.close();
		}
	}

	public User findByUsername(String username) {
		SqlSession session = getSqlSession();
		try {
			return getMapper(session).findByUsername(username);
		} finally {
			session.close();
		}
	}

	public void deleteByUsername(String username) {
		SqlSession session = getSqlSession();
		try {
			((UserMapper) session.getMapper(mapperClazz)).deleteByUsername(username);
		} finally {
			session.commit(true);
			session.close();
		}
	}

	public void deleteByID(int id) {
		SqlSession session = getSqlSession();
		try {
			((UserMapper) session.getMapper(mapperClazz)).deleteByID(id);
		} finally {
			session.commit(true);
			session.close();
		}
	}

	@Override
	protected UserMapper getMapper(SqlSession session) {
		return ((UserMapper) session.getMapper(mapperClazz));
	}
}
